package com.djx.provider.controller;

import com.djx.comm.controller.BaseController;
import com.djx.comm.entity.User;
import com.djx.provider.service.UserService;
import com.djx.comm.util.StringUtils;
import com.djx.comm.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 用户控制层
 * @author dingjunxiong
 */
@RestController
@RequestMapping("user/")
@Api(value = "用户接口", tags = "用户接口", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController extends BaseController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "根据id获取用户", notes = "根据id获取用户")
    @RequestMapping("getById")
    public Object getById(String id) throws Exception {
        return successObject(userService.queryById(id));
    }

    @ApiOperation(value = "分页获取用户", notes = "分页获取用户")
    @RequestMapping("getPage")
    public Object getPage(User user) throws Exception {
        return successObject(userService.queryPage(user));
    }

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @RequestMapping("add")
    public Object add(User user) throws Exception {
        try {
            user.setId(UUIDUtil.getUUID());
            user.setStatus(0);
            userService.add(user);
            return successMsg("添加成功");
        } catch (Exception e) {
            return successObject("添加失败", e.getMessage());
        }
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @RequestMapping("update")
    public Object update(User user) {
        try {
            if (StringUtils.isBlank(user.getId())) {
                throw new IllegalArgumentException("id不能为空");
            }
            userService.update(user);
            return successMsg("修改成功");
        } catch (Exception e) {
            return successObject("修改失败", e.getMessage());
        }
    }

    @ApiOperation(value = "导出用户", notes = "导出用户")
    @GetMapping("download")
    public Object downloadExcel(User user, HttpServletResponse response) {
        try {
            userService.downloadExcel(user, response);
            return successMsg("下载成功");
        } catch (Exception e) {
            return successObject("下载失败", e.getMessage());
        }
    }

    @ApiOperation(value = "导入用户", notes = "导入用户")
    @PostMapping("addList")
    public Object addList(MultipartFile file) {
        try {
            if (null == file || file.getSize() <= 0) {
                throw new IllegalArgumentException("文件不能为空");
            }
            userService.importExcel(file);
            return successMsg("添加成功");
        } catch (Exception e) {
            return successObject("添加失败", e.getMessage());
        }
    }
}
